load("@bazel_skylib//rules:diff_test.bzl", "diff_test")
load("//:defs.bzl", "artifact")

genquery(
    name = "testonly-deps",
    testonly = True,
    expression = "attr(testonly, 1, deps(@testonly_testing//:all, 0))",
    scope = [
        "@testonly_testing//:org_codehaus_mojo_animal_sniffer_annotations_1_17",
        "@testonly_testing//:org_checkerframework_checker_qual_2_5_2",
        "@testonly_testing//:com_google_j2objc_j2objc_annotations_1_1",
        "@testonly_testing//:com_google_guava_listenablefuture_9999_0_empty_to_avoid_conflict_with_guava",
        "@testonly_testing//:com_google_guava_guava_27_0_jre",
        "@testonly_testing//:com_google_guava_guava",
        "@testonly_testing//:org_codehaus_mojo_animal_sniffer_annotations",
        "@testonly_testing//:org_checkerframework_checker_qual",
        "@testonly_testing//:com_google_j2objc_j2objc_annotations",
        "@testonly_testing//:com_google_guava_listenablefuture",
        "@testonly_testing//:com_google_guava_failureaccess_1_0",
        "@testonly_testing//:com_google_guava_failureaccess",
        "@testonly_testing//:com_google_errorprone_error_prone_annotations_2_2_0",
        "@testonly_testing//:com_google_errorprone_error_prone_annotations",
        "@testonly_testing//:com_google_code_findbugs_jsr305_3_0_2",
        "@testonly_testing//:com_google_code_findbugs_jsr305",
        "@testonly_testing//:com_google_auto_value_auto_value_annotations_1_6_3",
        "@testonly_testing//:com_google_auto_value_auto_value_annotations",
        "@testonly_testing//:defs",
        "@testonly_testing//:outdated",
        "@testonly_testing//:pin",
    ],
)

genrule(
    name = "testonly-deps-sorted",
    testonly = 1,
    srcs = [":testonly-deps"],
    outs = ["testonly-deps-sorted.txt"],
    cmd = "cat $< | sed -e 's|^@@|@|g; s|\r||g' | sed -e 's|^@[^/]*[+~]|@|g; s|\r||g' | sort > $@",
)

diff_test(
    name = "testonly_artifacts_test",
    file1 = "testonly-deps.golden.unix",
    file2 = ":testonly-deps-sorted",
)

# https://github.com/coursier/coursier/issues/1792
# https://github.com/bazelbuild/rules_jvm_external/issues/433
genquery(
    name = "version_interval_deps",
    testonly = True,
    expression = "deps(@version_interval_testing//:io_grpc_grpc_netty_shaded, 2)",
    opts = [
        "--nohost_deps",
        "--noimplicit_deps",
    ],
    scope = ["@version_interval_testing//:io_grpc_grpc_netty_shaded"],
)

genrule(
    name = "version-interval-deps-sorted",
    testonly = 1,
    srcs = [":version_interval_deps"],
    outs = ["version-interval-deps-sorted.txt"],
    cmd = "cat $< | sed -e 's|^@@|@|g; s|\r||g' | sed -e 's|^@[^/]*[+~]|@|g; s|\r||g' | sort > $@",
)

diff_test(
    name = "version_interval_deps_test",
    file1 = "version-interval-deps.golden.unix",
    file2 = ":version-interval-deps-sorted.txt",
)

genquery(
    name = "forced-versions-deps",
    expression = "deps(@forcing_versions//:xyz_rogfam_littleproxy)",
    opts = [
        "--nohost_deps",
        "--noimplicit_deps",
    ],
    scope = ["@forcing_versions//:xyz_rogfam_littleproxy"],
)

genrule(
    name = "forced-versions-deps-sorted",
    testonly = 1,
    srcs = [":forced-versions-deps"],
    outs = ["forced-versions-deps-sorted.txt"],
    cmd = "cat $< | sed -e 's|^@@|@|g; s|\r||g' | sed -e 's|^@[^/]*[+~]|@|g; s|\r||g' | sort > $@",
)

diff_test(
    name = "forced-version-deps-test",
    file1 = "forced-versions-deps.golden.unix",
    file2 = ":forced-versions-deps-sorted.txt",
)

# This target will fail to build if we're not handling merging of maven.install
# tags properly, and if we don't handle multiple lock files properly.
java_library(
    name = "demonstrate_multiple_lock_files_are_okay",
    exports = [
        # Dependency from our own `MODULE.bazel
        artifact(
            "org.zeromq:jeromq",
            repository_name = "multiple_lock_files",
        ),
        artifact(
            "redis.clients:jedis",
            repository_name = "multiple_lock_files",
        ),
    ],
)

# Depend on something from the maven-resolved repo to ensure that things like the
# artifacts hash is consistent
genquery(
    name = "maven-resolved-is-ok",
    testonly = True,
    expression = "deps(@maven_resolved_with_boms//:org_seleniumhq_selenium_selenium_java)",
    opts = [
        "--nohost_deps",
        "--noimplicit_deps",
    ],
    scope = ["@maven_resolved_with_boms//:org_seleniumhq_selenium_selenium_java"],
    deps = [],
)
